package com.magic.gre.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.magic.gre.R;
import com.magic.gre.adapter.LearnFinishAdapter;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.entity.WordsDetailsBean;
import com.magic.gre.helper.Apphelper;
import com.magic.gre.mvp.contract.WordsDetailsContract;
import com.magic.gre.mvp.presenter.WordsDetailspresenterImpl;
import com.noname.lib_base_java.entity.MsgEvent;
import com.noname.lib_base_java.net.RxBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: Lmoumou
 * Date: 2019-01-08 16:00
 * 文件名: LearnFinishActivity
 * 描述:
 */
public class LearnFinishActivity extends BaseMVPActivity<WordsDetailspresenterImpl> implements WordsDetailsContract.View,
        LearnFinishAdapter.OnLearnFinishListener {

    @BindView(R.id.continue_tv)
    TextView continueTv;
    @BindView(R.id.finish_tv)
    TextView finishTv;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.mTextView)
    TextView mTextView;

    public static void startThis(Context context, String unitId, String unitName) {
        Intent intent = new Intent(context.getApplicationContext(), LearnFinishActivity.class);
        intent.putExtra("unitId", unitId);
        intent.putExtra("unitName", unitName);
        context.startActivity(intent);
    }

    private LearnFinishAdapter adapter;
    private List<WordsDetailsBean> dataList = new ArrayList<>();
    private String unitId;
    private String unitName;

    @Override
    protected int statusColorBy23() {
        return Color.parseColor("#F8F8F8");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_learn_finish;
    }

    @Override
    protected WordsDetailspresenterImpl creatPresenter() {
        return new WordsDetailspresenterImpl(this);
    }

    @Override
    protected void netOverride() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        unitName = getIntent().getStringExtra("unitName");
        unitId = getIntent().getStringExtra("unitId");
        titleTv.setText("成功学完 "+unitName);


        adapter = new LearnFinishAdapter(this, dataList);
        adapter.setOnLearnFinishListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(adapter);

        mPresenter.pUnitDetails(unitId);
        mPresenter.pCountToDay();
    }

    @OnClick(value = {R.id.continue_tv, R.id.finish_tv})
    protected void onClick(View view) {
        switch (view.getId()) {
            case R.id.continue_tv:
                finish();
                break;
            case R.id.finish_tv:
                if (Apphelper.isLogin()){
                    AnswerActivity.startThis(this, unitId);
                } else {
                    onNoLogin();
                }

                break;
        }
    }


    @Override
    public void vUnitDatails(List<WordsDetailsBean> wordsBeanList) {
        dataList.clear();
        dataList.addAll(wordsBeanList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void vNewWordsSave(int position) {
        WordsDetailsBean bean = dataList.get(position);
        if (!bean.isUnirpe()) {
            bean.setCooked(false);
        }
        bean.setUnirpe(!bean.isUnirpe());
        adapter.notifyDataSetChanged();
        RxBus.getInstance().post(new MsgEvent(MsgEvent.NEW_WORDS_CHANGE));
    }

    @Override
    public void vRipeWordsSave(int position) {
        WordsDetailsBean bean = dataList.get(position);
        if (!bean.isCooked()) {
            bean.setUnirpe(false);
        }
        bean.setCooked(!bean.isCooked());
        adapter.notifyDataSetChanged();
        RxBus.getInstance().post(new MsgEvent(MsgEvent.RIPE_WORDS_CHANGE));
    }

    @Override
    public void vRecordSave() {

    }

    @SuppressLint("SetTextI18n")
    @Override
    public void vCountToDay(String count) {
        mTextView.setText("今日已学"+count+"词");
    }

    @Override
    public void vCreat(String id) {

    }

    @Override
    public void vLastUnit(String lastId) {

    }

    @Override
    public void onNewWords(int position) {
        mPresenter.pNewWordsSave(dataList.get(position).getId(), position);
    }

    @Override
    public void onRipeWords(int position) {
        mPresenter.pRipeWordsSave(dataList.get(position).getId(), position);
    }

    @Override
    public void onDialogMiss() {

    }
}
